package com.zj.osm.view


import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.zj.osm.R

@Suppress("UNREACHABLE_CODE")
abstract class BaseFragment : Fragment() {
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val views = inflater.inflate(getContentViewLayoutID(), null)
        initView(views)
        return if (getContentViewLayoutID() != 0) {
            views
        } else {
            super.onCreateView(inflater, container, savedInstanceState)
        }
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initListener()
    }

    protected open fun initView(view: View) {

    }

    protected open fun startMyActivity(intent: Intent) {
        startActivity(intent)
        activity?.overridePendingTransition(R.anim.slide_in_from_right, R.anim.slide_out_from_right)

    }

    /**
     * 获取布局文件
     */
    protected abstract fun getContentViewLayoutID(): Int

    /**
     * 创建监听
     */
    abstract fun initListener()


    /**
     * 无参跳转
     *
     * @param clazz 需要跳转的类
     */
    protected fun startIntent(clazz: Class<*>?) {
        if (clazz == null) {
            return
        }
        val intent = Intent(context, clazz)
        startActivity(intent)
    }

    override fun startActivity(intent: Intent?) {
        super.startActivity(intent)
        if (intent != null) {
            getAnim()
        }
    }

    /**
     * 设置跳转和返回活动动画
     */
    private fun getAnim() {
        activity?.overridePendingTransition(R.anim.slide_in_from_right, R.anim.slide_out_from_right)
    }

}